using Microsoft.AspNetCore.Mvc;
using PandaWebAPI.Models.ef;
using PandaWebAPI.Services;

namespace PandaWebAPI.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class PandaController : ControllerBase
    {
        private readonly IPandaService _pandaService;

        public PandaController(IPandaService pandaService)
        {
            _pandaService = pandaService;
        }

        [HttpGet]
        public async Task<ActionResult<List<Panda>>> GetAllPandas()
        {
            var pandas = await _pandaService.GetAllAsync();
            return Ok(pandas);
        }

        [HttpGet("{id}")]
        public async Task<ActionResult<Panda>> GetPandaById(Guid id)
        {
            var panda = await _pandaService.GetByIdAsync(id);
            if (panda == null)
                return NotFound();

            return Ok(panda);
        }

        [HttpGet("base/{baseId}")]
        public async Task<ActionResult<List<Panda>>> GetPandasByBaseId(Guid baseId)
        {
            var pandas = await _pandaService.GetPandasByBaseIdAsync(baseId);
            return Ok(pandas);
        }

        [HttpGet("gender/{gender}")]
        public async Task<ActionResult<List<Panda>>> GetPandasByGender(string gender)
        {
            var pandas = await _pandaService.GetPandasByGenderAsync(gender);
            return Ok(pandas);
        }

        [HttpGet("birthdate")]
        public async Task<ActionResult<List<Panda>>> GetPandasByBirthDateRange([FromQuery] DateOnly startDate, [FromQuery] DateOnly endDate)
        {
            var pandas = await _pandaService.GetPandasByBirthDateRangeAsync(startDate, endDate);
            return Ok(pandas);
        }

        [HttpPost]
        public async Task<ActionResult<Panda>> CreatePanda(Panda panda)
        {
            try
            {
                var createdPanda = await _pandaService.CreateAsync(panda);
                return CreatedAtAction(nameof(GetPandaById), new { id = createdPanda.PandaId }, createdPanda);
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        [HttpPut("{id}")]
        public async Task<ActionResult<Panda>> UpdatePanda(Guid id, Panda panda)
        {
            if (id != panda.PandaId)
                return BadRequest();

            try
            {
                var updatedPanda = await _pandaService.UpdateAsync(panda);
                return Ok(updatedPanda);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        [HttpPut("{id}/image")]
        public async Task<ActionResult<Panda>> UpdatePandaImage(Guid id, [FromBody] string imageUrl)
        {
            try
            {
                var updatedPanda = await _pandaService.UpdatePandaImageAsync(id, imageUrl);
                return Ok(updatedPanda);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
        }

        [HttpDelete("{id}")]
        public async Task<ActionResult> DeletePanda(Guid id)
        {
            var result = await _pandaService.DeleteAsync(id);
            if (!result)
                return NotFound();

            return NoContent();
        }
    }
} 